package com.leilei.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {

    private static DruidDataSource dataSource = null;

    static {
        try {
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(com.leilei.util.DBConnTL.getInstance());
            dataSource.setDriverClassName(com.leilei.util.DBConnTL.getInstance().getProperty("driver"));
            dataSource.setUrl(com.leilei.util.DBConnTL.getInstance().getProperty("url"));
            dataSource.setUsername(com.leilei.util.DBConnTL.getInstance().getProperty("user"));
            dataSource.setPassword(com.leilei.util.DBConnTL.getInstance().getProperty("password"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DruidDataSource getDataSource(){
        return dataSource;
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void closeAll(Connection conn, Statement st, ResultSet rs){
        try {
            if (rs != null){
                rs.close();
            }
            if (st != null){
                st.close();
            }
            if (conn != null){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
